package com.db4o.odbgen.plugins;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import com.db4o.internal.odbgen.plugins.OptionList;


/**
 * 
 * Used by all plugins in the stack to report.
 * @author liviu
 *
 */
public class Log {
    public enum Severity{
        /**
         * Info message. This is used to inform about different stages of the
         * code generation process.
         */
        INFO,
        /**
         * Warning messages. This means that something goes wrong but the
         * code generation process is able to continue.
         */
        WARNING,
        /**
         * Error messages. All exceptions will have this severity.
         */
        ERROR,
        /**
         * Debug messages used to trace problems during code generation.
         */
        DEBUG
    }

    @SuppressWarnings("unused")/*TODO: remove SuppressWarnings*/
    private enum Option implements OptionList{
        /**
         * Enables/disables debug logging.
         * This is a true/false option.
         */
        DEBUGLOG("debugLog", "false"),
        /**
         * Enables/disables the logging system.
         * This is a true/false option.
         */
        DISABLELOG("disableLog", "false");

        private String _defaultValue;
        private String _name;

        Option(String name, String defaultValue){
            this._name = name;
            this._defaultValue = defaultValue;
        }
        
        /**
         * See {@link OptionList} for details.
         */
        @Override
        public String getName() {
            return this._name;
        }
        
        /**
         * See {@link OptionList} for details.
         */
        @Override
        public String getDefaultValue() {
            return this._defaultValue;
        }
    }
    
    public static void initialize(Properties _options) {
        //TODO: to be implemented
        
    }
    
    private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
    
    /**
     * Logs an exception.
     * The severity is set to ERROR.
     * @param e
     */
    public static void write(Exception e){
        System.out.println(e);
        System.out.flush();
        Log.write(Severity.ERROR, e.toString());
    }
    
    /**
     * Logs an arbitrary message.
     * @param severity The severity of the message.
     * @param format The formatted text.
     * @param args The arguments for the formatted text.
     */
    public static void write(Severity severity, String format, Object... args){
        String message = String.format(format, args);
        System.out.println(String.format("[%s] %s: %s", severity.toString(), Log.now(), message));
        System.out.flush();
    }
    
    /**
     * Returns the current date and time.
     */
    private static String now() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        return sdf.format(cal.getTime());

      }

}
